package com.infore.siteManagement.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.entity.AreaLevel;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IAreaLevelRespository;
import com.infore.siteManagement.select.QueryDispatcher;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.QueryUtil;
import com.infore.siteManagement.service.IAreaLevelService;
import com.infore.siteManagement.util.MyBeanUtils;
import com.infore.siteManagement.util.Page;
import com.infore.siteManagement.util.PageUtil;
import com.infore.siteManagement.util.ResultEntity;
/**
 * 区域等级服务实现类
 * @author 刘乐法
 *
 *
 */
@Service
public class IAreaLevelServiceImpl implements IAreaLevelService{

	@Autowired
	private IAreaLevelRespository iAreaLevelRespository;
	@Autowired
	private QueryDispatcher queryDispatcher;
	
	public ResultEntity queryAreaLevel(QueryParamDto queryParamDto) {
		if (org.springframework.util.StringUtils.isEmpty(queryParamDto)) {//无任何条件查询
			List<AreaLevel> list = iAreaLevelRespository.findAll();
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		//仅分页查询
		}else if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject()) && 
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getQueryCondition()) &&
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleCondition()) && 
				org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleSql())) {
			org.springframework.data.domain.Page<AreaLevel> pageable = iAreaLevelRespository.findAll(queryParamDto.getPageObject().getPageRequest());
			Page<AreaLevel> page = PageUtil.pageConvert(pageable);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		//只传属性不传字段的查询
		}else if (org.apache.commons.lang.StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) {
			return this.queryAll(queryParamDto);
		}
		//指定字段查询
		return queryDispatcher.dispatcher(queryParamDto, AreaLevel.class);
	}
	
	@SuppressWarnings("serial")
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		//判断是否有条件再判断是否需要分页,不分页必须传null
		if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject())) {
			//需要分页
			org.springframework.data.domain.Page<AreaLevel> pageAble =  iAreaLevelRespository.findAll(new Specification<AreaLevel>() {
				public Predicate toPredicate(Root<AreaLevel> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			}, queryParamDto.getPageObject().getPageRequest());
			Page<AreaLevel> page = PageUtil.pageConvert(pageAble);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		}else {
			List<AreaLevel> list = iAreaLevelRespository.findAll(new Specification<AreaLevel>() {
				public Predicate toPredicate(Root<AreaLevel> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			});
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		}
	}

	public ResultEntity validateAreaLevel(AreaLevel areaLevel) {
		String message = "验证区域等级成功";
		if (areaLevel.getAreaLevelCode().length() > 10) {
			message = "区域等级Code过长";
		}else if (areaLevel.getAreaLevelName().length() > 20) {
			message = "区域等级名称过长";
		}else if (areaLevel.getRemark().length() > 50) {
			message = "区域等级备注过长";
		}
		if (("验证区域等级成功").equals(message)) {
			return new ResultEntity(ResultStatus.SUCCESS,message);
		} else {
			return new ResultEntity(ResultStatus.ERROR,message);
		}
	}
	
	public ResultEntity editAreaLevel(AreaLevel areaLevel) {
		//AreaLevel的id不是自动生成的uuid，新增时需要自己按顺序定义id
		Boolean flag = false;//新增时为false
		//如果传入id不为空
		if (!StringUtils.isEmpty(areaLevel.getAreaLevelId())) {
			AreaLevel oldAreaLevel = iAreaLevelRespository.findById(areaLevel.getAreaLevelId()).orElse(null);
			if (!StringUtils.isEmpty(oldAreaLevel)) {
				MyBeanUtils.copyNonNullProperties(areaLevel, oldAreaLevel);
				areaLevel = oldAreaLevel;
			} 
//				else {
//				return new ResultEntity(ResultStatus.ERROR,ResultConstants.NO_SUCH_OBJECT);
//			}
			flag = true;
		}else {
			return new ResultEntity(ResultStatus.ERROR,"请输入区域等级id");
		}
		ResultEntity resultEntity = validateAreaLevel(areaLevel);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		try {
			iAreaLevelRespository.saveAndFlush(areaLevel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (flag) {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.UPDATE_SUCCESS);
		} else {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.INSERT_SUCCESS);
		}
	}

	public ResultEntity deleteAreaLevel(String ids) {
		//判断是否删除多个，含有“,”则拆分
		if (ids.contains(",")) {
			List<String> idsList = Arrays.asList(ids.split(","));
			iAreaLevelRespository.deleteByAreaLevelIdIn(idsList);
		} else {
			iAreaLevelRespository.deleteById(ids);
		}
		return new ResultEntity(ResultStatus.SUCCESS,Constants.DELETE_SUCCESS);
	}

}
